package edu.hrbu.trade01.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.hrbu.trade01.annotation.MyLog;
import edu.hrbu.trade01.entity.Firm;
import edu.hrbu.trade01.entity.VipFirm;
import edu.hrbu.trade01.service.FirmService;
import edu.hrbu.trade01.service.VipFirmService;
import edu.hrbu.trade01.util.ConstUtil;
import edu.hrbu.trade01.util.R;
import edu.hrbu.trade01.util.TableR;
import edu.hrbu.trade01.util.VipFirmUpdateVoUtil;
import edu.hrbu.trade01.vo.VipFirmUpdateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

import static edu.hrbu.trade01.util.ConstUtil.*;

/**
 * @author 徐登宇
 */
@Slf4j
@RestController
@RequestMapping("/firm")
public class FirmController {

    @Autowired
    private FirmService firmService;

    @Autowired
    private VipFirmService vipFirmService;

    /* 普通企业*/

    // 【普通企业】条件分页查询
    @RequestMapping("/common/list")
    public TableR commonList(@RequestParam(value = "page", defaultValue = "1") int page,
                       @RequestParam(value = "limit", defaultValue = "5") int limit,
                       @RequestParam(value = "searchParams", required = false) String firmStr) {

        Page<Firm> pageModel = firmService.getPageWithCondition(page, limit, firmStr, ConstUtil.NOT_VIP);
//        log.info("@"+pageModel);
        return TableR.ok(pageModel.getTotal(), pageModel.getRecords());
    }

    @MyLog(title = LOG_TITLE_UPDATE, businessType = LOG_TYPE_FIRM)
    @PostMapping("/common/update")
    public R commonUpdate(@RequestBody Firm firm) {
        firmService.updateById(firm);
        return R.ok();
    }

    @MyLog(title = "升级VIP", businessType = LOG_TYPE_FIRM)
    @Transactional
    @PostMapping("/common/to/vip")
    public R toVip(@RequestBody VipFirm vipFirm) {
        // 先将企业修改为vip
        Firm firm = new Firm();
        firm.setId(vipFirm.getFirmId());
        firm.setIsVip(IS_VIP);
        LambdaQueryWrapper<Firm> wrapperFirm = new LambdaQueryWrapper<>();
        wrapperFirm.eq(Firm::getId, vipFirm.getFirmId());
        firmService.update(firm, wrapperFirm);

        // 添加vip企业
        vipFirmService.save(vipFirm);
        return R.ok();
    }

    @MyLog(title = "批量删除", businessType = LOG_TYPE_FIRM)
    @PostMapping("/common/removeBatch")
    public R removeBatch(@RequestParam("ids") List<Long> ids) {
        firmService.removeBatchByIds(ids);
        return R.ok();
    }

    @MyLog(title = LOG_TITLE_DEL, businessType = LOG_TYPE_FIRM)
    @PostMapping("/common/remove")
    public R remove(@RequestParam("id") Long id) {
        firmService.removeById(id);
        return R.ok();
    }

    @MyLog(title = LOG_TITLE_SAVE, businessType = LOG_TYPE_FIRM)
    @PostMapping("/common/save")
    public R save(@RequestBody Firm firm){
        firmService.save(firm);
        return R.ok();
    }

    /* 普通企业*/


    //////////////////////////////////

    // 【VIP企业】条件分页查询
    @RequestMapping("/vip/list")
    public TableR vipList(@RequestParam(value = "page", defaultValue = "1") int page,
                             @RequestParam(value = "limit", defaultValue = "5") int limit,
                             @RequestParam(value = "searchParams", required = false) String firmStr) {

        Page<Firm> pageModel = firmService.getPageWithCondition(page, limit, firmStr, IS_VIP);
//        log.info("@"+pageModel);
        return TableR.ok(pageModel.getTotal(), pageModel.getRecords());
    }

    @RequestMapping("/vip/detail")
    public R detailByFirmId(@RequestParam("firmId") Long firmId) {
        LambdaQueryWrapper<VipFirm> wrapperVipFirm = new LambdaQueryWrapper<>();
        wrapperVipFirm.eq(VipFirm::getFirmId, firmId);
        VipFirm one = vipFirmService.getOne(wrapperVipFirm);
        return R.ok(one);
    }

    // TODO
    @MyLog(title = LOG_TITLE_UPDATE, businessType = LOG_TYPE_FIRM)
    @Transactional
    @PostMapping("/vip/update")
    public R update(@RequestBody VipFirmUpdateVo vipFirmUpdateVo) {
//        log.info("@@@:::"+vipFirmUpdateVo);

            HashMap<String, Object> map = VipFirmUpdateVoUtil.fitfirmAndVipFirm(vipFirmUpdateVo);
            Firm firm = (Firm) map.get("firm");
            VipFirm vipFirm = (VipFirm) map.get("vipFirm");
//            int n=1/0;
            firmService.updateById(firm);
            vipFirmService.updateById(vipFirm);

        return R.ok();
    }

    @MyLog(title = "更新图片", businessType = LOG_TYPE_FIRM)
    @PostMapping("/vip/updatePic")
    public R updatePic(@RequestBody VipFirm vipFirm) {
        vipFirmService.updateById(vipFirm);
        return R.ok();
    }




}
